package game.waypoint;
import graphics.system.Color;
import graphics.system.GLTools;

import java.io.Serializable;
import java.util.ArrayList;

import org.lwjgl.opengl.GL11;

import tools.Help;
import tools.Point;
public class Waypoint implements Serializable {
	public Point pos;
	public ArrayList<Waypoint> neighbors;
	public ArrayList<Float> distances;
	public int id;
	public float cdist;
	public Waypoint cpre;
	public boolean cvisited;
	boolean onFloor;
	public Waypoint(Point p) {
		this(p, -1, false);
	}
	public Waypoint(Point p, int id, boolean onFloor) {
		neighbors = new ArrayList<Waypoint>();
		distances = new ArrayList<Float>();
		this.id = id;
		this.pos = p;
		this.onFloor = onFloor;
	}
	public void addNeighbor(Waypoint w, float dist) {
		if (w.id == id)
			return;
		for (Waypoint p : neighbors) {
			if (p.id == w.id)
				return;
		}
		for (int i = 0; i < neighbors.size(); i++) {
			if (dist < distances.get(i)) {
				neighbors.add(i, w);
				distances.add(i, dist);
				return;
			}
		}
		neighbors.add(w);
		distances.add(dist);
	}
	public void removeNeighbor(Waypoint w) {
		int i = neighbors.indexOf(w);
		if (i != -1) {
			distances.remove(i);
			neighbors.remove(i);
		}
	}
	public void draw() {
		GL11.glEnable(GL11.GL_LINE_STIPPLE);
		GL11.glLineStipple(1, (short) 0xf0f0);
		for (Waypoint p : neighbors) {
			if (p.id > id) {
				GLTools.drawLine(this.pos, p.pos, Color.yellow.withAlpha(0.2f));
			}
		}
		GL11.glDisable(GL11.GL_LINE_STIPPLE);
		if (onFloor)
			GLTools.drawCircle(pos, 5, 0, Color.red, false);
		else
			GLTools.drawCircle(pos, 5, 0, Color.blue, false);
		if (cvisited) {
			GLTools.drawCircle(pos, 150, 0, Color.green, false);
		}
		// GLTools.drawText(pos, ""+id+" "+cdist, Color.red);
	}
	public String getFileString() {
		String s = id + ";" + pos.toFileString() + ";" + String.valueOf(onFloor);
		for (Waypoint n : neighbors) {
			if (n.id < id) {
				s = s + ";" + n.id;
			}
		}
		return s;
	}
}